/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DB_REDIS.Redis_Rd;
import DB_REDIS.Redis_W;
import Model.Request.ClientRequest;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import share.KeysDefinition;
import share.ShareMacros;

/**
 *
 * @author LinhTA
 */
public class DeviceController extends Controller{
    
     private static Logger logger_ = Logger.getLogger(DeviceController.class);
    
     @Override  
    public void handle(ClientRequest request, HttpServletResponse resp) 
    {
          try {
            prepareHeader(resp);
            
            if(request._method.equals("get"))
               getDevice(request, resp); 
            else
                addDevice(request, resp);
            
        } catch (Exception ex) {
           logger_.error("Profile.handle:" + ex.getMessage() + ", Username:" + request._uid.toString(), ex);
        }
    }
    
    @Override
    protected JSONObject defaultResponse_False()
    {
       JSONObject data = new JSONObject();
      data.put(ShareMacros.DEVICEID, "");
       data.put(ShareMacros.FACEID, "");
       data.put(ShareMacros.MEID, "");
        data.put(ShareMacros.TOKEN, "");
        data.put(ShareMacros.CONFIG, "");
        data.put(ShareMacros.PHONENUMBER, "");
        
        return  data;
    }
    
     public void addDevice ( ClientRequest req, HttpServletResponse resp )
    {
         
        
        String deviceId = req._data.get(ShareMacros.DEVICEID);
        
      
        String uId = req._uid;//ids.get(ShareMacros.ID);
        
        String token = req._data.get(ShareMacros.TOKEN);
        String config = req._data.get(ShareMacros.CONFIG);
        String phone = req._data.get(ShareMacros.PHONENUMBER);
        
        
        String key = KeysDefinition.getKeyDevice(uId, deviceId);
        Map<String,String> data = new HashMap<>();
        data.put(ShareMacros.TOKEN, token);
        data.put(ShareMacros.CONFIG, config);
        data.put(ShareMacros.PHONENUMBER, phone);
        
        String check;
        if (Redis_W.getInstance().hset(key, data) != "-1")
            check = "true";
        else
            check = "false";
        
        JSONObject mapjson = new JSONObject();
       mapjson.put(ShareMacros.SUSSCES, check);
       out(mapjson.toJSONString(), resp);
    }
    
    public void getDevice( ClientRequest req, HttpServletResponse resp )
    {
        
        
         String deviceId = req._data.get(ShareMacros.DEVICEID);
        
        String meID = req._meID;//ids.get(ShareMacros.MEID);
        String faceID = req._fbID;//ids.get(ShareMacros.FACEID);
        String uid = req._uid;//ids.get(ShareMacros.ID);
         
        
        String key = KeysDefinition.getKeyDevice(uid, deviceId);
        Map<String,String> data = new HashMap<>();
        
       data =  Redis_Rd.getInstance().hget(key);
       data.put(ShareMacros.DEVICEID, deviceId);
       data.put(ShareMacros.FACEID, faceID);
       data.put(ShareMacros.MEID, meID);
       
       JSONObject mapjson = new JSONObject();
       mapjson.putAll(data);
       
        out(mapjson.toJSONString(), resp);
    }
    
}
